<?php
namespace app\admin\controller;
use app\admin\model\system\Version;
use app\admin\model\user\User;
use app\common\controller\Admin;
use Curl\CurlHttpRequest;
use think\helper\Time;
use think\Request;
use app\common\annotation\NodeAnnotation;
/**
 * Class Administrator
 * @package app\admin\controller
 * @NodeAnnotation(
 *     title="系统入口",
 *     parent="admin/system.setting",
 *     is_menu=0,
 *     is_view=0,
 *     auth=0
 * )
 */
class Index extends Admin
{

    /**
     * Class Administrator
     * @package app\admin\controller
     * @NodeAnnotation(
     *     title="系统系统首页",
     *     parent="admin/system.setting",
     *     is_menu=0,
     *     is_view=0,
     *     auth=0
     * )
     */
    public function index (Request $request)
    {
        return $this->fetch();
    }

    /**
     * Class Administrator
     * @package app\admin\controller
     * @NodeAnnotation(
     *     title="系统系统首页",
     *     parent="admin/system.setting",
     *     is_menu=0,
     *     is_view=0,
     *     auth=0
     * )
     */
    public function home()
    {
        $this->assign([
            "userCount"  => $this->getusercount(),
            "versionList"=> Version::order("update_time","desc")->limit(10)->cache(true,3000)->select(),
            "tongji"     => $this->tongji()
        ]);
        return $this->fetch();
    }

    //获取用户统计
    protected function getusercount()
    {
        $userModel = new User();
        return [
            "countUser"      => $userModel->cache(true,3000)->count(),
            "toDayCountUser" => $userModel->whereTime("create_time","today")->cache(true,3000)->count(),
            "weekCountUser"  => $userModel->whereTime("create_time","week")->cache(true,3000)->count(),
            "monthCount"     => $userModel->whereTime("create_time","month")->cache(true,3000)->count()
        ];
    }


    //网站流量统计
    protected function tongji()
    {
        list($todayStart,$todayEnd) = Time::today();
        list($yesTodayStart,$yesTodayEnd) = Time::yesterday();
        $setting = get_option("baidutongji_setting");
        $param = json_encode([
            "header" =>$setting,
            "body" => [
                "site_id" => $setting['site_id'],
                "start_date" => date('Ymd',$yesTodayStart),
                "end_date" => date('Ymd',$todayEnd),
                "metrics" => "pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time,trans_count, ",
                "method" => "overview/getTimeTrendRpt",
            ]
        ]);
        $tongji = json_decode((new CurlHttpRequest())->curlHttpPost('https://api.baidu.com/json/tongji/v1/ReportService/getData',$param,[
            CURLOPT_HTTPHEADER=>[
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($param)
            ],
        ]),true);
        return $tongji;
    }






}